package com.ruoyi.common.core.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.*;


/**
 * @Description: 线程池配置
 * @Author: j.zh
 * @Date: 2024/3/16
 * @Version: 1.0
 **/
@Configuration
@EnableAsync
public class ThreadPoolConfig {

    @Bean(name = "auto")
    public ThreadPoolTaskExecutor customThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        // 设置核心线程数和最大线程数
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(15);

        // 设置线程空闲超时时间
        executor.setKeepAliveSeconds(60);

        // 设置队列为有界队列，容量为10
        ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(100);


        executor.setQueueCapacity(1000);
        executor.setRejectedExecutionHandler(new CustomRejectedExecutionHandler());

        executor.setThreadNamePrefix("auto-thread-");
        executor.initialize();
        return executor;
    }

    /**
     * 添加到SellerStoreProduct 在线商品表
     * @return
     */
    @Bean(name = "publishToSSPMq")
    public ThreadPoolTaskExecutor publishToSSPMq() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        // 设置核心线程数和最大线程数
        executor.setCorePoolSize(3);
        executor.setMaxPoolSize(5);

        // 设置线程空闲超时时间
        executor.setKeepAliveSeconds(60);

        // 设置队列为有界队列，容量为10
        ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(100);


        executor.setQueueCapacity(1000);
        executor.setRejectedExecutionHandler(new CustomRejectedExecutionHandler());

        executor.setThreadNamePrefix("publishToSellerStoreProductQueue-thread-");
        executor.initialize();
        return executor;
    }

    /**
     * 添加到SellerProduct 个人商品表
     * @return
     */
    @Bean(name = "publishToSPMq")
    public ThreadPoolTaskExecutor publishToSPMq() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        // 设置核心线程数和最大线程数
        executor.setCorePoolSize(3);
        executor.setMaxPoolSize(5);

        // 设置线程空闲超时时间
        executor.setKeepAliveSeconds(60);

        // 设置队列为有界队列，容量为10
        ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(100);


        executor.setQueueCapacity(1000);
        executor.setRejectedExecutionHandler(new CustomRejectedExecutionHandler());

        executor.setThreadNamePrefix("publishToSellerProductQueue-thread-");
        executor.initialize();
        return executor;
    }

    /**
     * 创建店铺分组
     * @return
     */
    @Bean(name = "createStoreGroupMq")
    public ThreadPoolTaskExecutor createStoreGroupMq() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        // 设置核心线程数和最大线程数
        executor.setCorePoolSize(3);
        executor.setMaxPoolSize(5);

        // 设置线程空闲超时时间
        executor.setKeepAliveSeconds(60);

        // 设置队列为有界队列，容量为10
        ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(100);


        executor.setQueueCapacity(1000);
        executor.setRejectedExecutionHandler(new CustomRejectedExecutionHandler());

        executor.setThreadNamePrefix("publishToSellerProductQueue-thread-");
        executor.initialize();
        return executor;
    }

    /**
     * 速卖通上架后操作
     * @return
     */
    @Bean(name = "aeProductSetDataMq")
    public ThreadPoolTaskExecutor aeProductSetDataMq() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        // 设置核心线程数和最大线程数
        executor.setCorePoolSize(3);
        executor.setMaxPoolSize(5);

        // 设置线程空闲超时时间
        executor.setKeepAliveSeconds(60);

        // 设置队列为有界队列，容量为10
        ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(100);


        executor.setQueueCapacity(1000);
        executor.setRejectedExecutionHandler(new CustomRejectedExecutionHandler());

        executor.setThreadNamePrefix("aeProductSetDataQueue-thread-");
        executor.initialize();
        return executor;
    }

    // 自定义拒绝策略
    static class CustomRejectedExecutionHandler implements RejectedExecutionHandler {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            // 在被拒绝时执行自定义操作
            System.out.println("Auto Task Rejected: " + r.toString());
        }
    }
}
